package org.example.controller;

import org.example.entity.Book;
import org.example.entity.Order;
import org.example.service.IBookService;
import org.example.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/orders")
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private IBookService bookService;

//    @PostMapping
//    public boolean add(@RequestBody Order order){
//        orderService.createOrder(order);
//        return true;
//    }

    @GetMapping("/books")
    public List<Book> books(){
        return bookService.getAll(1);
    }

//    @PostMapping("/order")
//    public boolean order(Long bookId,Integer userId){
//        Order order = new Order();
//        order.setUserId(userId);
//        order.setBookId(bookId);
//        orderService.createOrder(order);
//        return true;
//    }

    //异步生成订单
    @PostMapping
    public boolean add(@RequestBody Order order){
        orderService.asynCreateOrder(order);
        return true;
    }

    @GetMapping("/sk")
    public String sk(Long bookId, Integer userId){
        boolean success =  orderService.secondsKill(bookId,userId);
        return success ? "秒杀成功" : "秒杀失败";
    }
}
